package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.atguigu.exam.service.impl.FileUploadServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "轮播图管理接口",description = "轮播图相关操作，包括图片上传，轮播图增删改查，状态管理功能")
@RestController
@RequestMapping("/api/banners")
@CrossOrigin
public class BannerController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图", description = "获取所有轮播图列表，包括启用和禁用的，供管理后台使用")
    @GetMapping("/list")
    public Result<List<Banner>> findAll(){
        List<Banner> list = bannerService.list();
        return Result.success(list);
    }

    @Operation(summary = "获取启用轮播图 ")
    @GetMapping("/active")
    public Result getActiveBannerList(){
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.eq(Banner::getIsActive,true);
        List<Banner> list = bannerService.list(bannerLambdaUpdateWrapper);
        return Result.success(list,"获取启动的轮播图成功");
    }


    @Operation(summary = "根据ID查询轮播图")
    @GetMapping("/{id}")
    public Result getBannerById(@PathVariable Long id){
        Banner banner = bannerService.getById(id);
        return Result.success(banner,"根据ID查询轮播图成功");
    }

    @Operation(summary = "添加轮播图")
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner){
        boolean save = bannerService.save(banner);
        if(save){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @Operation(summary = "更新轮番图")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner){
        boolean b = bannerService.updateById(banner);
        if(b){
            return Result.success("更新成功");
        }else {
            return Result.error("更新失败");
        }
    }


    @Operation(summary = "根据ID删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean b = bannerService.removeById(id);
        if (b) {
            return Result.success("根据ID删除轮播图成功");
        } else {
            return Result.error("根据ID删除轮播图失败");
        }
    }


    @Operation(summary = "切换轮番图状态")
    @PutMapping("/toggle/{id}")
    public Result toggleActiveStatus(@PathVariable Long id ,@RequestParam Boolean isActive){
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.set(Banner::getIsActive,isActive).eq(Banner::getId,id);
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        if(update){
            return Result.success("更新轮播图状态成功");
        }else {
            return Result.error("更新轮播图状态失败");
        }
    }

    @Operation(summary = "上传轮播图图片")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        String fileUrl = fileUploadService.uploadFile(file);
        return Result.success(fileUrl,"上传轮播图图片成功");
    }
}
